<?php
class Morderin extends CI_Model {
    protected $_table;
    public function __construct() {
        parent::__construct();
        $this->_table="order_in";
    }
    
    public function listAll($amount, $start) {
        $query=$this->db->query("select o.*, prov.prov_name from order_in as o
                                    inner join providers as prov on prov.prov_id = o.prov_id
                                    order by o.oi_date desc
                                    limit $start, $amount");
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAll() {
        return $this->db->count_all($this->_table);
    }
    
    public function search($prov, $datefrom, $dateto, $pricefrom, $priceto, $amount, $start) {
        $p=$d=$pr="'1=1'";
        if($prov!=0) $p="o.prov_id = $prov";
        if($datefrom!="" and $dateto!="") $d="oi_date between '".$datefrom."' and '".$dateto."'";
        if($pricefrom!="" and $priceto!="") $pr="oi_price between $pricefrom and $priceto";
        $str="select o.*, prov.prov_name from order_in as o
                inner join providers as prov on prov.prov_id = o.prov_id
                where $p and $d and $pr
                order by o.oi_date desc
                limit $start, $amount";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAllResults($prov, $datefrom, $dateto, $pricefrom, $priceto) {
        $p=$d=$pr="'1=1'";
        if($prov!=0) $p="o.prov_id = $prov";
        if($datefrom!="" and $dateto!="") $d="oi_date between '".$datefrom."' and '".$dateto."'";
        if($pricefrom!="" and $priceto!="") $pr="oi_price between $pricefrom and $priceto";
        $str="select count(*) as 'count' from order_in as o
                inner join providers as prov on prov.prov_id = o.prov_id
                where $p and $d and $pr
                order by o.oi_date desc";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function getById($id) {
        $this->db->where("oi_id", $id);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function insert($data) {
        $this->db->insert($this->_table, $data);
        return $this->db->insert_id();
    }
    
    public function update($data, $id) {
        $this->db->where("oi_id", $id);
        $this->db->update($this->_table, $data);
    }
    
    public function del($id) {
        $this->db->where("oi_id", $id);
        $this->db->delete($this->_table);
    }
}
?>
